﻿using Abp.Application.Services.Dto;
using Abp.Domain.Repositories;
using Abp.Specifications;
using AppFramework.DinosaursParkMes.Product;
using AppFramework.DinosaursParkMes.ProductionReturn.DTO;
using AppFramework.DinosaursParkMes.TicketTask;
using AppFramework.DinosaursParkMes.TicketTask.DTO;
using Microsoft.AspNetCore.Mvc;
using Microsoft.EntityFrameworkCore;
using System;
using System.Collections.Generic;
using System.ComponentModel.DataAnnotations;
using System.Linq;
using System.Runtime.InteropServices;
using System.Text;
using System.Threading.Tasks;

namespace AppFramework.DinosaursParkMes.ProductionReturn
{
    /// <summary>
    /// 生产退料管理
    /// </summary>
    public class ProductionReturninfoAppService : AppFrameworkAppServiceBase, IProductionReturninfoAppService
    {
        private readonly IRepository<ProductionReturninfo> repository;
        private readonly IRepository<ReturnofMaterialinfo> ReturnofMaterialinforepository;
        private readonly IRepository<Memberinfo> memberinforepository;
        private readonly IRepository<TeamorGroupinfo> teamorGroupinforepository;
        private readonly IRepository<AppFramework.DinosaursParkMes.CodingRuleModel.ProductWorkModels> productWorkModelsrepository;//生产工单表
        private readonly IRepository<Productinfo> productinforepository;//产品表
        public ProductionReturninfoAppService(IRepository<ProductionReturninfo> repository, IRepository<ReturnofMaterialinfo> returnofMaterialinforepository = null, IRepository<Memberinfo> memberinforepository = null, IRepository<TeamorGroupinfo> teamorGroupinforepository = null, IRepository<CodingRuleModel.ProductWorkModels> productWorkModelsrepository = null, IRepository<Productinfo> productinforepository = null)
        {
            this.repository = repository;
            ReturnofMaterialinforepository = returnofMaterialinforepository;
            this.memberinforepository = memberinforepository;
            this.teamorGroupinforepository = teamorGroupinforepository;
            this.productWorkModelsrepository = productWorkModelsrepository;
            this.productinforepository = productinforepository;
        }
        /// <summary>
        /// 生产退料新增
        /// </summary>
        /// <param name="dto"></param>
        /// <returns></returns>
        [HttpPost]
        public async Task<int> AddProductionReturn(ProductionReturnRequestDTO dto)
        {
            var data = ObjectMapper.Map<ProductionReturninfo>(dto);
            
            if(dto.children==null)
            {
                dto.children = new List<ProductionReturnItem>();
            }
            foreach (var item in dto.children)
            {
                var wuliaoitem = ObjectMapper.Map<ReturnofMaterialinfo>(item);
                wuliaoitem.Returnedmaterialnumber = data.Returnedmaterialnumber;
                await ReturnofMaterialinforepository.InsertAsync(wuliaoitem);
            }
            await repository.InsertAsync(data);
            return 1;
        }
        /// <summary>
        /// 删除
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        /// <exception cref="NotImplementedException"></exception>
        [HttpDelete]
        public async Task<int> DeleteProductionReturn(string id)
        {
            string[] ids = id.Split(',');
            foreach (var item in ids)
            {
                var list = repository.FirstOrDefault(Convert.ToInt32(item));
                if(list.IsDeleted==false)
                {
                    list.IsDeleted = true;
                }
                
            }
            return 1;
        }

        /// <summary>
        /// 生产退料列表
        /// </summary>
        /// <param name="dto"></param>
        /// <returns></returns>
        /// <exception cref="NotImplementedException"></exception>
        [HttpGet]
        public async Task<PagedResultDto<ProductionReturnResponseDTO>> ProductionReturnList()
        {
            PagedResultDto<ProductionReturnResponseDTO> result = new PagedResultDto<ProductionReturnResponseDTO>();
            var list = from a in  repository.GetAll()
                       select new ProductionReturnResponseDTO
                       {
                           Id= a.Id,
                           Missionnumber= a.Missionnumber,
                           Processname= a.Processname,
                           Processroute=a.Processroute,
                           Remark= a.Remark,
                           Returndate= a.Returndate,
                           Returnedmaterialname= a.Returnedmaterialname,
                           Returnedmaterialnumber= a.Returnedmaterialnumber,
                           Returnperson= a.Returnperson,
                           Stats= a.Stats,
                           Taskname= a.Taskname,
                           Workordername= a.Workordername,
                           Workordernumber= a.Workordernumber,
                       };
            //if (!string.IsNullOrEmpty(dto.Returnedmaterialnumber))
            //{
            //    list = list.Where(m => m.Returnedmaterialnumber.Equals(dto.Returnedmaterialnumber) || m.Returnedmaterialname.Equals(dto.Returnedmaterialnumber));
            //}
            //if (!string.IsNullOrEmpty(dto.Workordernumber))
            //{
            //    list = list.Where(m => m.Workordernumber.Equals(dto.Workordernumber) || m.Workordername.Equals(dto.Workordernumber));
            //}
            //if (!string.IsNullOrEmpty(Convert.ToString(dto.Returndate)))
            //{
            //    list = list.Where(m => m.Returndate.Equals(dto.Returndate));
            //}
            //if (!string.IsNullOrEmpty(dto.Missionnumber))
            //{
            //    list = list.Where(m => m.Missionnumber.Equals(dto.Missionnumber) || m.Taskname.Equals(dto.Missionnumber));
            //}
            //if (!string.IsNullOrEmpty(dto.Returnperson))
            //{
            //    list = list.Where(m => m.Returnperson.Equals(dto.Returnperson));
            //}
            //if (!string.IsNullOrEmpty(dto.Processroute))
            //{
            //    list = list.Where(m => m.Processroute.Equals(dto.Processroute));
            //}
            //if (!string.IsNullOrEmpty(dto.Processname))
            //{
            //    list = list.Where(m => m.Processname.Equals(dto.Processname));
            //}
            //result.Items = await list.OrderByDescending(a => a.Id).Skip((dto.pageindex - 1) * dto.pagesize).Take(dto.pagesize).ToListAsync();
            //result.TotalCount = list.Count();
            result.Items = await list.ToListAsync();
            return result;
            
        }
        /// <summary>
        /// 根据班组获取班组成员
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>

        public async Task<List<GroupMemberResponseDTO>> SelectMember(int id)
        {
            var list = from a in teamorGroupinforepository.GetAll()
                       join b in memberinforepository.GetAll() on a.Id equals b.Classid
                       select new GroupMemberResponseDTO
                       {
                           MemberName=b.Membername
                       };
            return await list.ToListAsync();
        }
        /// <summary>
        /// 根据工单列表获取工单编号
        /// </summary>
        /// <param name="code"></param>
        /// <returns></returns>
        /// <exception cref="NotImplementedException"></exception>
        [HttpGet]
        public dynamic SelectWorker(string code)
        {
            var list = productWorkModelsrepository.GetAll().Where(m => m.WorkOrderNumber == code).FirstOrDefault().WorkOrderNumber;
            return  list;
        }

        /// <summary>
        /// 退料
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        /// <exception cref="NotImplementedException"></exception>
        [HttpPost]
        public async  Task<int> UptStates(UptStatesId dto)
        {
            var list = repository.GetAll().Where(m => m.Id == dto.Id).FirstOrDefault();
            if (list.IsDeleted == false)
            {
                list.IsDeleted = true;
            }
            return 1;
        }
        /// <summary>
        /// 工单列表
        /// </summary>
        /// <returns></returns>
        /// <exception cref="NotImplementedException"></exception>
        [HttpGet]
        public async Task<List<TicketTaskSelectResponseDTO>> WorkOrderShow()
        {
            var product = productinforepository.GetAll();

            var list = from a in productWorkModelsrepository.GetAll()
                       select new TicketTaskSelectResponseDTO
                       {
                           Id=a.Id,
                           Workordernumber=a.WorkOrderNumber,
                           ProductName=a.ProductName,
                           WorkOrderName=a.WorkOrderName,
                           ProductCode=a.ProductNumber,
                           Specification=a.Specification,
                           ProductTypeName=a.ProductType,
                           Units=a.Unit
                       };
            return await list.ToListAsync();
        }
    }
}
